/**
* Title: 阿里云短信服务
* Author: lauwen
* E-mail: me@luxuewen.com
* Created_at: 2021/7/14 10:00
* M O T T O : "Computers do not solve problems, they execute solutions." - Laurent Gasser
 */

package ali_cloud

import (
	config2 "gin-cli/utils/config"
	openapi "github.com/alibabacloud-go/darabonba-openapi/client"
	dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
	"github.com/alibabacloud-go/tea/tea"
)

type AliCloudSMS struct {
	PhoneNumbers string
	SignName string
	TemplateCode string
	TemplateParam string
}

/**
* Title: 初始化短信客户端
* Author: lauwen
* E-mail: me@luxuewen.com
 */
func initSmsClient() (_result *dysmsapi20170525.Client, err error) {
	accessKeyId := config2.Get("aliCloud.accessKeyId").(string)
	accessKeySecret := config2.Get("aliCloud.accessKeySecret").(string)
	endPoint := config2.Get("aliCloud.sms.endpoint").(string)
	config := &openapi.Config{
		AccessKeyId: &accessKeyId,
		AccessKeySecret: &accessKeySecret,
	}

	config.Endpoint = &endPoint
	_result = &dysmsapi20170525.Client{}
	_result, err = dysmsapi20170525.NewClient(config)
	return _result, err
}

/**
* Title: 发送短信
* Author: lauwen
* E-mail: me@luxuewen.com
 */
func Send (sms AliCloudSMS) (err error) {
	client, err := initSmsClient()
	if err != nil {
		return err
	}

	sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
		PhoneNumbers: tea.String(sms.PhoneNumbers),
		SignName: tea.String(sms.SignName),
		TemplateCode: tea.String(sms.TemplateCode),
		TemplateParam: tea.String(sms.TemplateParam),
	}

	_, err = client.SendSms(sendSmsRequest)

	return err
}